package com.googlecode.loframework.controller
{
	import com.googlecode.loframework.model.ProjectDataProxy;
	import com.googlecode.loframework.view.IComponent;
	import com.googlecode.loframework.view.IPageHolder;
	import com.googlecode.loframework.view.PageHolderMediator;
	import com.googlecode.loframework.view.buttons.BackButtonMediator;
	import com.googlecode.loframework.view.buttons.ForwardButtonMediator;
	import com.googlecode.loframework.view.buttons.IBackButton;
	import com.googlecode.loframework.view.buttons.IForwardButton;
	import com.googlecode.loframework.view.components.accessibility.ITripleA;
	import com.googlecode.loframework.view.components.accessibility.TripleAMediator;
	import com.googlecode.loframework.view.components.pagecounter.IPageCounter;
	import com.googlecode.loframework.view.components.pagecounter.PageCounterMediator;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	public class StartCommand extends SimpleCommand
	{
		/**
		 * Execute the command.
		 * @param notification Body should contain a FWT instance.
		 * @throws Error if incorrect body found in notification.
		 */	
		 
		 	
		override public function execute(notification:INotification):void
		{
			var appRef:Application = notification.getBody() as Application;
			
			// test correct body
			if(appRef == null)
			{
				throw new Error("Notification body should contain an Application instance.");
			}
			
			// register proxy instances
			registerProxies();
			
			// register mediators
			// do this after the proxies so data is available
			registerMediators(appRef);
		}
		
		/**
		 * Register PureMVC proxy instances. 
		 */		
		protected function registerProxies():void
		{
			facade.registerProxy(new ProjectDataProxy());
		}
		
		
		protected function registerMediators(appRef:Application):void
		{
			// find the PageHolder component(s)
			searchTree(appRef as UIComponent);

		}
		protected function searchTree(child:UIComponent):void
		{
			for(var i:uint=0; i<child.numChildren; i++)
			{
				if(child.getChildAt(i) as UIComponent != null)
				{	
					if(child.getChildAt(i) is IComponent)
					{
						allocateMediator(child.getChildAt(i) as IComponent);
									
					}
					
					searchTree((child.getChildAt(i) as UIComponent));	
				}
			}
		}
		
		protected function allocateMediator(component:IComponent):void
		{	
	
			if(component is IForwardButton)
			{
				facade.registerMediator(new ForwardButtonMediator(component));
			}
			
			if(component is IBackButton)
			{
				facade.registerMediator(new BackButtonMediator(component));
			}
			
			if(component is IPageHolder)
			{
				facade.registerMediator(new PageHolderMediator(component));
			}
			
			if(component is IPageCounter)
			{
				facade.registerMediator(new PageCounterMediator(component));
			}
			
			if(component is ITripleA)
			{
				facade.registerMediator(new TripleAMediator(component));
			}
			
		}
	}
}